tests: shunit2: add test for _jshn_append leading space fix
authorPetr Štetiar <[email protected]>
Thu, 13 Nov 2025 10:38:41 +0000 (10:38 +0000)
committerPetr Štetiar <[email protected]>
Thu, 13 Nov 2025 10:47:22 +0000 (10:47 +0000)
Verifies the fix in commit 2065c3909935 ("libubox: Drop extraneous space
when appending values to variable") that prevents leading spaces when
appending to empty variables.

 test_jshn_append_no_leading_space
 ASSERT:expected:<foo> but was:< foo>
 ASSERT:expected:<first second> but was:< first second>

References: #16
Signed-off-by: Petr Štetiar <[email protected]>
tests/shunit2/tests.sh

index b27775a6b12890bb3196c47326eb9b1ca21dee8c..bb02fcd8ffdc7cf0c6729886c9341f6d400ea54d 100755 (executable)
@@ -284,4 +284,25 @@ test_cmd_return() {
 %VAR% ow"
 }
 
+test_jshn_append_no_leading_space() {
+       JSON_PREFIX="${JSON_PREFIX:-}"
+       . ../../sh/jshn.sh
+
+       # Test appending to empty variable - should not have leading space
+       var=''
+       _jshn_append var 'foo'
+       assertEquals "foo" "$var"
+
+       # Test appending to non-empty variable - should have space separator
+       var='bar'
+       _jshn_append var 'foo'
+       assertEquals "bar foo" "$var"
+
+       # Test multiple appends to empty variable
+       var=''
+       _jshn_append var 'first'
+       _jshn_append var 'second'
+       assertEquals "first second" "$var"
+}
+
 . ./shunit2/shunit2